home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4475 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.9 KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: free space malloced to auto vars??
  5. Date: 03 Feb 1996 21:18:08 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Feb3141808@qcd.lanl.gov>
  8. References: <4eqf8m$smv@gsusgi1.gsu.edu>
  9. NNTP-Posting-Host: qcd.lanl.gov
  10. Mime-Version: 1.0
  11. Content-Type: text
  12. In-reply-to: matmrlx@gsusgi1.gsu.edu's message of 1 Feb 1996 08:29:58 -0500
  13.  
  14. In article <4eqf8m$smv@gsusgi1.gsu.edu> matmrlx@gsusgi1.gsu.edu
  15. (Michael R. Lauer) writes: 
  16. <snip>
  17.    The general question is: if you malloc space for a pointer with automatic
  18.    storage, should/must you free that space before leaving that scope?
  19.  
  20. I assume from your later example that you are talking about the scope
  21. of the pointer which holds the last or only copy of an address
  22. pointing to a malloc'ed region: not the scope where the malloc was
  23. actually done.
  24.  
  25. Freeing it is a good idea :-) Technically, you don't need to free it
  26. ... if you don't free it, you continue to hold on to the space without
  27. any means of either using it or freeing it. I guess that's what you
  28. meant anyways.
  29.  
  30.    This seems obvious but it is giving me trouble. In this code:
  31.  
  32.    {
  33.        struct dirent *dp;
  34.        if ((dp = readdir() != NULL)
  35.        {
  36.        }
  37.        if (dp)                /* this seems to cause
  38. a segmentation fault */ 
  39.            free(dp);        /* when the enclosing function
  40. returns */ 
  41.    }
  42.  
  43.    So readdir returns a pointer to the dirent structure--it is mallocing the
  44.    space. When I leave this scope I AM done with dp and the struct. 
  45.    Don't I have to free(dp)? The "if(dp) free(dp)" at the end of the
  46. scope caused  
  47.    a segmentation fault when I returned from the enclosing function. It 
  48.    apparently works fine without the free(), but I am worried about memory 
  49.    leakage.
  50.  
  51. Apart from the typo, the code fragment seems correct. The if(dp) part
  52. is not needed in an ANSI environment, pre-ANSI libraries sometimes
  53. crashed on free(NULL).
  54.  
  55. Are you absolutely sure that readdir is malloc-ing the space? Why
  56. don't you ask in a group more suited to that question. Anything can
  57. happen if you free something that has not been malloced. Say,
  58. something called opendir() mallocs space and something called
  59. readdir() returns a pointer to somewhere in this malloced place. Then
  60. if you free the pointer returned by readdir(), all hell can break
  61. loose. 
  62.  
  63. Note that the only thing that can be freed is a pointer returned by
  64. malloc or calloc or realloc, not other pointers pointing inside a
  65. malloc'ed region.
  66.  
  67. Cheers
  68. Tanmoy
  69. --
  70. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  71. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  72. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  73. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  74. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  75. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  76.